

package com.XSQS.storage.util;

import java.io.*;

/**
 *
 * @author Jofee.Chan
 */
public class DocInfoPage implements Page{
    
    private short status;
    private int nextPage;
    private int docID;
    private int dataPage;
    private int stringPage;
    //private short nameLength;  //
    private short length;
    private String docName;
    private String[] symbols;
    
    public final static short PAGE_SIZE = 1024;
    public final static short MAX_DATA_LENGTH = 500;
    /** Creates a new instance of DocInfoPage */
    public DocInfoPage() {
    }
    
    public DocInfoPage(byte[] data){
        buildPage(data);
    }
    
    public void buildPage(byte[] data){
        if(data.length == PAGE_SIZE){
            DataInputStream dis = new DataInputStream(new ByteArrayInputStream(data));
            try {
                setStatus(dis.readShort());
                setNextPage(dis.readInt());
                setDocID(dis.readInt());
                setDataPage(dis.readInt());
                setStringPage(dis.readInt());
                int nameLength = dis.readShort();
                setLength(dis.readShort());
                char[] namechar = new char[nameLength];
                for(int i = 0; i < nameLength; i++){
                    namechar[i] = dis.readChar();
                }
                setDocName(String.valueOf(namechar));
                char[] symbolchar = new char[getLength()];
                for(int i = 0; i < getLength(); i++){
                    symbolchar[i] = dis.readChar();
                }
                String s = String.valueOf(symbolchar);
                setSymbols(s.split("#"));
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
    }
    
    public byte[] toByteArray(){
        ByteArrayOutputStream baos = new ByteArrayOutputStream(this.PAGE_SIZE);
        DataOutputStream dos = new DataOutputStream(baos);
        try {
            dos.writeShort(getStatus());
            dos.writeInt(getNextPage());
            dos.writeInt(getDocID());
            dos.writeInt(getDataPage());
            dos.writeInt(getStringPage());
            dos.writeShort(getDocName().length());
            dos.writeShort(getLength());
            dos.writeChars(getDocName());
            for(int i = 0; i < getSymbols().length; i++){
                dos.writeChars(symbols[i] + '#');
            }
            byte[] blank = new byte[this.PAGE_SIZE - dos.size()];
            dos.write(blank);
        } catch (IOException ex) {
            ex.printStackTrace();
            return null;
        }
        return baos.toByteArray();
    }
    
    public short getStatus() {
        return status;
    }
    
    public void setStatus(short status) {
        this.status = status;
    }
    
    public int getDocID() {
        return docID;
    }
    
    public void setDocID(int docID) {
        this.docID = docID;
    }
    
    public int getNextPage() {
        return nextPage;
    }
    
    public void setNextPage(int nextPage) {
        this.nextPage = nextPage;
    }
    
    public int getDataPage() {
        return dataPage;
    }
    
    public void setDataPage(int dataPage) {
        this.dataPage = dataPage;
    }
    
    public int getStringPage() {
        return stringPage;
    }
    
    public void setStringPage(int stringPage) {
        this.stringPage = stringPage;
    }
    
    public short getLength() {
        return length;
    }
    
    public void setLength(short length) {
        this.length = length;
    }
    
    public String getDocName() {
        return docName;
    }
    
    public void setDocName(String docName) {
        this.docName = docName;
    }
    
    public String[] getSymbols() {
        return symbols;
    }
    
    public void setSymbols(String[] symbols) {
        this.symbols = symbols;
    }
    
    public short getType() {
        return Page.DOCINFOPAGE;
    }
    
    public String toString(){
        StringBuffer sb = new StringBuffer();
        sb.append("Status: " + this.status + "     ");
        sb.append("NextPage: " + this.nextPage + "\n");
        sb.append("DocID: " + this.docID + "     ");
        sb.append("DocName: " + this.docName + "\n");
        sb.append("DataPage: " + this.dataPage + "     ");
        sb.append("StringPage: " + this.stringPage + "\n");
        sb.append("DataLength: " + this.length + "\n");
        for(int i = 1; i <= this.symbols.length; i ++){
            sb.append(i + ". " + this.symbols[i-1] + "   ");
            if(i % 5 == 0) sb.append('\n');
        }
        return sb.toString();
    }
    
}
